// pages/o2oSearch/o2oSearch.js
var location = require("../../../mybehaviors/location/location");

var app = getApp();
Page({
  behaviors: [location],

  /**
   * 页面的初始数据
   */
  data: {
    city: '',
    point: {
      lat: '',
      lng: ''
    },
    stores: [],
    inputs: "",
    //more
    isLoadMore: true,
    page: 1,
    total_page: 0,
    showPage: false,

    ok_subs_select: null, //筛选器确定请求的二级选项参数
    successLocation: false, //是否已定位成功，筛选器需要定位信息为参数
    isOptionMenuMore: false, //是否为筛选器的加载更多
    activeType: '',
    subs_select: '',
    openTemplate: 0, //门店列表样式
    screenMenu: [{ name: "商品", api: "plugin.store-cashier.frontend.store.goods.get-store-goods-list" },{ name: "门店", api: "plugin.store-cashier.frontend.store.goods.get-store-goods-by-title" }],
    screenIndex: 1,
    screenShow: false,
    is_sales_display: 0, //门店 is_sales_display  0 不显示销量  1显示销量
    imgPath: app.globalData.imgPath
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let point = JSON.stringify(this.data.point);
    return {
      title: '',
      path: '/packageC/o2o/o2oSearch/o2oSearch?city=' + this.data.city + '&point=' + point,
    };
  },
  setScreenShow() {
    this.setData({
      screenShow: !this.data.screenShow
    });
  },
  setScreenIndex(evt){
    let index = evt.currentTarget.dataset.index;
    if (index == this.data.screenIndex) return;
    this.setData({
      screenIndex: index,
      screenShow: false
    });
    this.gotoSearch();
  },
  gotoSearch() {
    var that = this;
    that.setData({
      page: 1,
      successLocation: true
    });
    let urlStr = app.getNetAddresss(this.data.screenMenu[this.data.screenIndex].api);
    let point = app.qqMapTransBMap(parseFloat(that.data.point.lng), parseFloat(that.data.point.lat));
    let json = {};
    if(this.data.screenIndex == 0){
      json = {
        search:{keyword:this.data.inputs ? this.data.inputs : ''},
        lng: point.lng,
        lat: point.lat,
      }
    }else{
      json = {
        kwd: this.data.inputs ? this.data.inputs : '',
        lng: point.lng,
        lat: point.lat,
        city_name: this.data.city,
        page:this.data.page
      };
    }
    
    app._postNetWork({
      url: urlStr,
      data:json,
      success:  (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            isLoadMore: true,
            total_page: res.data.last_page,
            is_sales_display: res.data.is_sales_display
          });
          if (!that.data.total_page) {
            that.setData({
              total_page: 0
            });
          }
          if(this.data.screenIndex == 0){
            this.setData({
              listData:res.data.data
            })  
          }else{
            that.setData({
              stores: res.data.data,
              openTemplate: res.data.store_list_style || 0
            });
          }
        } else {
          that.setData({
            stores: []
          });
        }
        that.setData({
          showPage: true
        });
      },
      fail: function (res) {
        console.log(res);
        that.setData({
          showPage: true
        });
      }
    });
  },
  //获取更多数据
  getMoreData() {
    var that = this;

    let _url = !this.data.isOptionMenuMore ? this.data.screenMenu[this.data.screenIndex].api : 'plugin.store-cashier.frontend.store.goods.get-store-search';
    let urlStr = app.getNetAddresss(_url);

    let point = app.qqMapTransBMap(parseFloat(that.data.point.lng), parseFloat(that.data.point.lat));
    let json = {};
    if (this.data.isOptionMenuMore) {
      json ={
        lng:point.lng,
        lat:point.lat,
        city_name:point.city_name,
        is_type:point.this.data.activeType,
      }
      //筛选器的加载更多
      if (this.data.activeType == "classify") {
        json.category_id = this.data.subs_select;
      }
      if (this.data.activeType == "distance") {
        json.is_distance = this.data.subs_select;
      }
    } else {
      // urlStr += '&kwd=' + (this.data.inputs ? this.data.inputs : '');
      if(this.data.screenIndex == 0){
        json = {
          search:{keyword:this.data.inputs ? this.data.inputs : ''},
          lng: point.lng,
          lat: point.lat,
        }
      }else{
        json = {
          kwd: this.data.inputs ? this.data.inputs : '',
          lng: point.lng,
          lat: point.lat,
          city_name: this.data.city,
          page:this.data.page
        };
      }
    }

    this.setData({
      isLoadMore: false
    }); // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      });
      json.page = this.data.page;
      app._postNetWork({
        url: urlStr,
        data:json,
        success: (resdata)=> {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              isLoadMore: true,
              is_sales_display: res.data.is_sales_display
            });
            var myData = res.data.data;
            if(this.data.screenIndex == 0){
              that.setData({
                listData: that.data.listData.concat(myData)
              });
            }else{
              that.setData({
                stores: that.data.stores.concat(myData)
              });
            }
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false
            }); // 防止多次请求分页数据
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  inpbtn(e) {
    let val = e.detail.value;
    this.setData({
      inputs: val
    });
  },
  searchSet(e) {
    this.setData({
      screenIndex: e.detail
    });
    if (this.options.city && this.options.city !== '1') {
      if (this.options.city) {
        this.setData({
          city: this.options.city
        });
      }
      if (this.options.point) {
        this.setData({
          point: JSON.parse(this.options.point)
        });
      }
      this.gotoSearch();
    } else {
      this._getLocation((mapdata, e) => {
        this.setData({
          city:  mapdata.address_component.city || '',
          point:  {
            lng: e.lng,
            lat: e.lat
          }
        });
        this.gotoSearch();
      });
    }
  },
  changeOptiones(e) {
    this.data.isOptionMenuMore = true;
    this.data.activeType = e.detail.activeType;
    this.data.subs_select = e.detail.subs_select;
    let that = this;
    let point = app.qqMapTransBMap(parseFloat(that.data.point.lng), parseFloat(that.data.point.lat));
    let json = {
      lng: point.lng,
      lat: point.lat,
      city_name: this.data.city,
      is_type: e.detail.activeType
    };
    if (e.detail.activeType == "classify") {
      json.category_id = e.detail.subs_select;
    }
    if (e.detail.activeType == "distance") {
      json.is_distance = e.detail.subs_select;
    }
    that.setData({
      stores: []
    });
    let _url = "plugin.store-cashier.frontend.store.goods.get-store-search";
    let urlStr = app.getNetAddresss(_url);
    app._getNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            isLoadMore: true,
            page:1,
            total_page: res.data.last_page
          });
          if (!that.data.total_page) {
            that.setData({
              total_page: 0
            });
          }
          that.setData({
            ok_subs_select: e.detail.subs_select,
            stores: res.data.data
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  }
});